package com.yjf.question.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjf.question.entity.*;
import com.yjf.question.mapper.AnswerQuestionMapper;
import com.yjf.question.service.AnswerPaperChoiceService;
import com.yjf.question.service.AnswerPaperService;
import com.yjf.question.service.AnswerQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
    public class AnswerQuestionServiceImpl extends ServiceImpl<AnswerQuestionMapper, AnswerQuestion> implements AnswerQuestionService {
        @Autowired
        private AnswerPaperChoiceService answerPaperChoiceService;
        @Autowired
        private AnswerPaperService answerPaperService;

        @Override
        @Transactional
        public void saveCommit(CommitParm parm) {
            //保存answer_question表
            AnswerQuestion answerQuestion = new AnswerQuestion();
            answerQuestion.setOpenid(parm.getOpenid());
            answerQuestion.setQuestionId(parm.getQuestionId());
            answerQuestion.setCreateTime(new Date());
            this.baseMapper.insert(answerQuestion);
            //保存answer_paper表
            //前端传来的试题
            List<PaperPrm> paperList = parm.getPaperList();
            List<AnswerPaper> list = new ArrayList<>();
            for(int i=0;i<paperList.size();i++){
                AnswerPaper answerPaper = new AnswerPaper();
                BeanUtils.copyProperties(paperList.get(i),answerPaper);
                answerPaper.setOpenid(parm.getOpenid());
                list.add(answerPaper);
                //保存answer_paper_choice表
                //单选和多选需要单独保存选项
                if(paperList.get(i).getPaperType().equals("1")){
                    AnswerPaperChoice choice = new AnswerPaperChoice();
                    choice.setOpenid(parm.getOpenid());
                    choice.setPaperId(paperList.get(i).getPaperId());
                    choice.setChioceId(Long.valueOf(paperList.get(i).getPaperValue()));
                    answerPaperChoiceService.save(choice);
                }
                if(paperList.get(i).getPaperType().equals("2")){
                    String regx = ",";
                    String[] strings = paperList.get(i).getPaperValue().split(regx);
                    List<AnswerPaperChoice> choiceList = new ArrayList<>();
                    for(int j=0;j<strings.length;j++){
                        AnswerPaperChoice choice = new AnswerPaperChoice();
                        choice.setOpenid(parm.getOpenid());
                        choice.setPaperId(paperList.get(i).getPaperId());
                        choice.setChioceId(Long.valueOf(strings[j]));
                        choiceList.add(choice);
                    }
                    answerPaperChoiceService.saveBatch(choiceList);
                }
            }
            answerPaperService.saveBatch(list);
        }
    }

